import React from 'react';
import { Modal, Icon, Tooltip } from 'antd';
import { useRequest, defaultFormatResult } from '@/hooks';
import { getUnStartInterviewer, syncInterviewStatus, getInterviewRecordStatus } from '@/pages/Recruitment/service';

// 多面试官录音相关
// 裂开
export default ({ arrangeId }) => {
    const { run: runUnStartList } = useRequest(() => getUnStartInterviewer(arrangeId), {
        manual: true,
        ...defaultFormatResult
    });

    // 开始录音前轮询查询未到达页面的面试官
    const onBeforeStart = () =>
        new Promise((resolve, reject) => {
            runUnStartList().then(unStartList => {
                if (!unStartList?.length) {
                    resolve();
                    return;
                }
                Modal.confirm({
                    icon: <Icon type="exclamation-circle" />,
                    title: <p>{unStartList.join('、')}还未开始面试，是否开始录音</p>,
                    content: (
                        <div style={{ position: 'relative' }}>
                            <Tooltip
                                title={
                                    <>
                                        <p>Q:系统如何判别面试官开始面试？</p>
                                        <p>A:面试官未打开&quot;面试记录&quot;页面时，认为其未开始面试</p>
                                    </>
                                }
                            >
                                <Icon style={{ position: 'absolute', top: 30, left: -33 }} type="question-circle" />
                            </Tooltip>
                        </div>
                    ),
                    okText: '确定',
                    cancelText: '取消',
                    onOk() {
                        resolve();
                    },
                    onCancel() {
                        reject();
                    }
                });
            });
        });

    const { run: runSync, cancel: cancelSync } = useRequest(() => syncInterviewStatus({ arrangeId }), {
        manual: true,
        pollingInterval: 3000
    });

    const { data: recordStatus, run: runGet, cancel: cancelGet } = useRequest(
        () => getInterviewRecordStatus(arrangeId),
        {
            ...defaultFormatResult,
            manual: true,
            pollingInterval: 3000
        }
    );

    // 他人正在录音时的信息
    const recordStatusMsg = recordStatus?.employeeName ? `${recordStatus.employeeName}正在录音…` : null;

    React.useEffect(() => {
        runSync();
        runGet();
        return () => {
            cancelSync();
            cancelGet();
        };
    }, []);

    return { onBeforeStart, recordStatusMsg };
};
